package bean.admin.status;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.sql.ResultSet;

public class AdminStatusBean {
    int code;
    String message;
    String status;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String changeStaus(String token,String pStatus){
        JDBCConnection jdbc = new JDBCConnection();
        String tokenArry[] = token.split("#");
        try {
            String id = checkPermissions(tokenArry[0],tokenArry[2],jdbc);
            if(!id.equals("")){
                if(!"".equals(pStatus)&&pStatus!=null)  {
                    jdbc.executeUpdate("UPDATE parkinglot set lotstatus=\""
                            +pStatus+ "\" WHERE lotid=\""+id+"\";");
                    status = pStatus;
                }else{
                    ResultSet rs = jdbc.executeQuery("select lotstatus from parkinglot where lotid=\"" +id+"\";");
                    rs.next();
                    status = rs.getString("lotstatus");
                }
                code = 0;
                message = "成功";
            }
            else {
                code = 200;
                message = "token错误或者用户无权限";
            }
        }catch (Exception e){
            code = 100;
            message = "token错误";
        }
        return JSON.toJSONString(this);
    }
    private String checkPermissions( String admin_id, String admin_pwd, JDBCConnection jdbc){
        String querySQL = "select lotid,adminpsd from lotmanager where adminid = '"+admin_id+"'";
        ResultSet rs = jdbc.executeQuery(querySQL);
        try {
            rs.next();
            if(admin_pwd.equals(rs.getString("adminpsd"))) return rs.getString("lotid");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
